const { createCanvas } = require('@napi-rs/canvas');
const fs = require('fs');

// HSV转RGB函数（H: 0-360, S: 0-100, V: 0-100）
function hsvToRgb(h, s, v) {
    h = h % 360;
    s = s / 100;
    v = v / 100;
    const c = v * s;
    const x = c * (1 - Math.abs((h / 60) % 2 - 1));
    const m = v - c;
    let r, g, b;

    if (h >= 0 && h < 60) {
        [r, g, b] = [c, x, 0];
    } else if (h < 120) {
        [r, g, b] = [x, c, 0];
    } else if (h < 180) {
        [r, g, b] = [0, c, x];
    } else if (h < 240) {
        [r, g, b] = [0, x, c];
    } else if (h < 300) {
        [r, g, b] = [x, 0, c];
    } else {
        [r, g, b] = [c, 0, x];
    }

    return [
        Math.round((r + m) * 255),
        Math.round((g + m) * 255),
        Math.round((b + m) * 255)
    ];
}

// 创建画布
const width = 1920 * 2;
const height = 1080;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
const data = imageData.data;

// 填充图像数据
for (let x = 0; x < width; x++) {
    const h = (x / width) * 360;  // H值均匀分布
    const [r, g, b] = hsvToRgb(h, 100, 100);  // S=100%, V=100%

    // 填充整列
    for (let y = 0; y < height; y++) {
        const index = (y * width + x) * 4;
        data[index] = r;         // R
        data[index + 1] = g;    // G
        data[index + 2] = b;    // B
        data[index + 3] = 255;  // Alpha通道
    }
}

// 绘制并保存图片
ctx.putImageData(imageData, 0, 0);
const buffer = canvas.toBuffer('image/png');
fs.writeFileSync('hsv_gradient.png', buffer);
console.log('图片已生成：hsv_gradient.png');